//
//  ChangeNameController.swift
//  DSP
//
//  Created by tucici on 2024/7/4.
//

import UIKit

class ChangeNameController: DSAlertController {
    @IBOutlet var input: UITextField!
    var text = ""
    var block:((String)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        input.autocorrectionType = .no
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        input.text = text
    }
    @IBAction func actionForOK(_ sender: UIButton) {
        if (input.text ?? "") == ""{
            CLHUD("It can't be empty",type: .error, callBack: nil)
            return
        }
        block?(input.text ?? "")
        hidden()
    }
    
}

//MARK: - UITextFieldDelegate
extension ChangeNameController : UITextFieldDelegate{
    func textFieldDidChangeSelection(_ textField: UITextField) {
        if textField.markedTextRange != nil{return}
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let text = textField.text ?? ""
        if string == "" {return true}
        if !string.isNumberOrAlphabet(){return false}
        if text.count < 6 {return true}
        return false
    }
}
